<?php

define("DB_SERV", "elm.cis.uncw.edu");
define("DB_USER", "boinc");
define("DB_PASS", "border8max");

class DDASConn
{
	var $conn = null;
	var $rs = null;
	var $row = null;
	var $error = "";
	
	/*
	* Constructor calls the open function if it is
	* provided with a database name.
	*/
	function ddasConn($database)
	{
		if ($database)
		{
			return $this->open($database);
		}

		return true;
	}
	
	/*
	* Open function connects to the server and opens
	* the specified database.
	*/
	function open($database)
	{
		$this->conn = mysql_connect(DB_SERV, DB_USER, DB_PASS);

		if (!$this->conn)
		{
			$this->conn = null;
			$this->rs = null;
			$this->row = null;
			$this->error = "Database connection failed...";

			return false;
		}
		
		if (!mysql_select_db($database, $this->conn))
		{
			$this->conn = null;
			$this->rs = null;
			$this->row = null;
			$this->error = "Could not select database $database...";

			return false;
		}

		$this->error = "";

		return true;
	}
	
	/*
	* Closes the database connection and sets all
	* properties to their default values.
	*/
	function close()
	{
		mysql_close();
		$this->conn = null;
		$this->rs = null;
		$this->row = null;
		$this->error = "";
	}
	
	/*
	* Execute query with no record set.
	*/
	function execute($query)
	{
		if (stristr($query, ';'))
		{	// very simple sql injection test
			$this->error = "Invalid character in query...";

			return false;
		}

		if (mysql_query($query))
		{
			return true;
		}

		return false;
	}

	/*
	* Executes a query on the selected database.
	*/
	function query($query)
	{	
		if (stristr($query, ';'))
		{	// very simple sql injection test
			$this->error = "Invalid character in query...";

			return false;
		}
		
		$this->rs = mysql_query($query);

		if (!$this->rs)
		{
			$this->rs = null;
			$this->row = null;
			$this->error = "Query did not generate results...";

			return false;
		}
	
		$this->error = "";

		return true;
	}
	
	/*
	* Move to first row in result set.
	*/
	function moveFirst()
	{
		if (!$this->rs)
		{
			$this->rs = null;
			$this->row = null;
			$this->error = "No result set exists...";

			return false;
		}
		
		// first move to zero
		mysql_data_seek($this->rs, 0);
		
		return $this->moveNext();
	}
	
	/*
	* Move to next row in result set.
	*/
	function moveNext()
	{
		if (!$this->rs)
		{
			$this->rs = null;
			$this->row = null;
			$this->error = "No result set exists...";

			return false;
		}

		$this->row = mysql_fetch_assoc($this->rs);

		if (!$this->row)
		{	// end of result set
			$this->error = "";
			$this->row = null;

			return false;
		}
		
		return true;
	}

	/*
	* Returns the value for a particular column of the current
	* row of a result set.
	*/
	function getValue($column)
	{
		if (!$this->row)
		{
			$this->error = "Could not fetch row assoc...";
			$this->row = null;

			return null;
		}

		$value = $this->row[$column];

		if (!$value)
		{
			$this->error = "Column not present in results...";

			return null;
		}
		
		$this->error = "";

		return $value;
	}
	
	/*
	* Return index of last inserted record.
	*/
	function getLastIndex()
	{
		$recordId = mysql_insert_id();

		if ($recordId)
		{
			return $recordId;
		}

		return null;
	}

}

/*
* Returns userId if the email address has been registered
* as a researcher and the password is correct.  Otherwise, 
* returns 0.
*/
function sqlSelectUserId($email, $password)
{
	$conn = new DDASConn("ddas");

	$query = "SELECT userId " . 
				"FROM ddas_user " . 
				"WHERE email = '$email' " .
					"AND password = '$password'";

	$conn->query($query);
	$conn->moveFirst();

	$userId = $conn->getValue("userId");
	
	$conn->close();

	return $userId ? $userId : 0;
}

/*
* Add problem to database and return problemId.
*/
function sqlInsertProblem($userId, $numRows, $numColX, $numColY, $numResults)
{
	if (!is_numeric($userId) || $userId <= 0)
	{	// userId is not valid
		return 0;
	}
	
	if (!is_numeric($numRows) || $numRows <= 0)
	{	// row count is not valid
		return 0;
	}

	if (!is_numeric($numColX) || $numColX <= 0)
	{	// independant variable column count is invalid
		return 0;
	}

	if (!is_numeric($numColY) || $numColY <= 0)
	{	// dependant variable column count is invalid
		return 0;
	}

	if (!is_numeric($numResults) || $numResults <= 0)
	{	// use single result by default
		$numResults = 1;
	}

	$query = "INSERT INTO ddas_problem " . 
				"(userId, numRows, numColX, numColY, numResults, status) " .
				"VALUES " . 
				"($userId, $numRows, $numColX, $numColY, $numResults, 0)";

	$conn = new DDASConn("ddas");

	$conn->execute($query);

	$problemId = $conn->getLastIndex();

	$conn->close();

	if ($problemId)
	{
		return $problemId;
	}
	
	return 0;
}

?>
